import axios from 'axios';

const service = axios.create({
    baseURL: '/api',
    timeout: 30000
})

service.interceptors.response.use(function (response) {
    let res = response.data;
    if (res.code == 200) {
        return res.data;
    } else {

        return Promise.reject(res);
    }
}, (error) => {
    return Promise.reject(error);
});

service.interceptors.request.use(function (config) {
    if (JSON.parse(localStorage.getItem('userinfo'))) {
        let token = JSON.parse(localStorage.getItem('userinfo')).token;
        if (token) {
            config.headers.Authorization = 'Bearer ' + token;
            return config;
        }
       
        if (config.method == 'post') {
            config.headers['content-type'] = 'application/json;charset=UTF-8';
            return config;
        }
    }
    return config
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


export default service